<?php
namespace app\index\controller;

use app\index\model\News as NewsModel;
use app\index\model\NewsType;
use think\Request;

class News extends Home
{
    public function index(Request $request)
    {
    	$map = [];
    	$map['n.status'] = 1;
    	if (!empty($this->request->param('type_id'))) {
            $map['n.type_id'] = $this->request->param('type_id');
        } 

    	//动态分类
    	$type_list = NewsType::order('sort ASC')->field('id, name, sort')->cache(true, 60)->select();

    	//动态列表
    	$list = NewsModel::alias('n')
    		->where($map)
    		->join('jk_news_type t', 'n.type_id = t.id', 'LEFT')
    		->field('n.id, n.title, n.pic, n.descs, n.create_at, t.name')
    		->paginate(20);

		// dump(json_decode( json_encode( $list),true));exit;
        $title = '集团动态';

        return view('index' ,compact('type_list', 'list', 'title'));
    }

    public function detail()
    {
        $id = $this->request->param('id');
        // dump($id);

        //动态分类
        $type_list = NewsType::order('sort ASC')->field('id, name, sort')->cache(true, 60)->select();

        //详情
        $detail = NewsModel::alias('n')
            ->where('n.id', $id)
            ->join('jk_news_type t', 'n.type_id = t.id', 'LEFT')
            ->field('n.id, n.title, n.browse, n.descs, n.create_at, t.name, n.type_id, n.content')
            ->find();

        //上一条
        $shang_info = NewsModel::where('id<'.$id)->order('create_at DESC')->where('status', 1)->field('id, title')->find();
        //下一条
        $xia_info = NewsModel::where('id>'.$id)->order('create_at DESC')->where('status', 1)->field('id, title')->find();

        NewsModel::where('id', $id)->setInc('browse');
        $title = $detail['name'].'-'.$detail['title'];


        return view('detail', compact('type_list', 'detail', 'shang_info', 'xia_info', 'title'));
    }
}
